package com.qingding.activity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.qingding.R;

import java.lang.reflect.Field;

import com.qingding.utils.MNumberPicker;

public class RemindTime extends AppCompatActivity {
    Button affirm;
    MNumberPicker numberPicker;
    Context mContext=this;
    Button my_design;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remind_time);
        my_design=(Button) findViewById(R.id.other_day);
    }
    public void finish(View view) {
        this.finish();
    }
    public void choose_alert_time(View view){
        final Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.number_picker, null);
        numberPicker = (MNumberPicker) contentView.findViewById(R.id.numberPicker);
        affirm = (Button) contentView.findViewById(R.id.affirm);
        numberPicker.setValue(1);
        numberPicker.setMaxValue(50);
        numberPicker.setMinValue(1);
        numberPicker.setFocusable(false);
        numberPicker.setFocusableInTouchMode(false);
        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); // 关闭编辑模式
        setNumberPickerDividerColor(numberPicker);
        bottomDialog.setContentView(contentView);
        bottomDialog.setCanceledOnTouchOutside(true);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.show();
        affirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,""+numberPicker.getValue(),Toast.LENGTH_SHORT).show();
                bottomDialog.hide();
                my_design.setText("提前"+numberPicker.getValue()+"天");
            }
        });
    }
    private void setNumberPickerDividerColor(NumberPicker number) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(number, new ColorDrawable(ContextCompat.getColor(this, R.color.black)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

}
